package cn.minis.beans;

import cn.minis.beans.factory.AutowireCapableBeanFactory;
import cn.minis.beans.factory.config.BeanDefinition;
import cn.minis.beans.factory.support.SimpleBeanFactory;
import cn.minis.beans.factory.xml.XmlBeanDefinitionReader;
import cn.minis.beans.processor.AutowiredAnnotationBeanPostProcessor;

public class ClassPathXmlApplicationContext implements BeanFactory {
    
    BeanFactory beanFactory;
    
    //context负责整合容器的启动过程，读外部配置，解析Bean定义，创建BeanFactory
    public ClassPathXmlApplicationContext(String fileName) {
        this(fileName, false);
    }
    
    //context负责整合容器的启动过程，读外部配置，解析Bean定义，创建BeanFactory
    public ClassPathXmlApplicationContext(String fileName, boolean isRefresh) {
        Resource resource = new ClassPathXmlResource(fileName);
        
        AutowireCapableBeanFactory beanFactory = new AutowireCapableBeanFactory();
        beanFactory.addBeanPostProcessor(new AutowiredAnnotationBeanPostProcessor());
        
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        
        reader.loadBeanDefinitions(resource);
        this.beanFactory = beanFactory;
        if (isRefresh) {
            beanFactory.refresh();
        }
    }
    
    
    //context再对外提供一个getBean，底下就是调用的BeanFactory对应的方法
    @Override
    public Object getBean(String beanName) throws BeansException {
        return this.beanFactory.getBean(beanName);
    }
    
    //@Override
    //public void registerBeanDefinition(BeanDefinition beanDefinition) {
    //    this.beanFactory.registerBeanDefinition(beanDefinition);
    //}
    
/*    @Override
    public void registerBean(String beanName, Object obj) {
        this.beanFactory.registerBean(beanName, obj);
    }*/
    
    
    @Override
    public Boolean containsBean(String name) {
        return this.beanFactory.containsBean(name);
    }
    
    @Override
    public boolean isSingleton(String name) {
        return false;
    }
    
    @Override
    public boolean isPrototype(String name) {
        return false;
    }
    
    @Override
    public Class<?> getType(String name) {
        return null;
    }
    
}
